package com.tdrc.shop.controller;

import com.tdrc.common.beans.ResultCode;
import com.tdrc.common.beans.User;
import com.tdrc.common.controller.BaseController;
import com.tdrc.common.core.redis.RedisUtil;
import com.tdrc.common.service.UserService;
import com.tdrc.common.util.CommonUtil;
import com.tdrc.common.util.JsonResult;
import com.tdrc.shop.beans.Good;
import com.tdrc.shop.beans.GoodOrder;
import com.tdrc.shop.service.GoodService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author dpf
 * @version 1.0
 * @date 2020-4-28 11:21
 * @instruction ...
 */
@RestController
@RequestMapping("/good")
public class GoodController extends BaseController {

    @Resource
    private GoodService goodService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private UserService userService;
    /**
     * 单机版
     * 第一个版本的秒杀功能,利用synchronized关键字
     * @param good
     * @return
     */
    @PostMapping("/seckill")
    public   Map<String,Object> seckill(Good good){
        Map<String,Object> map = new HashMap<>();
        // 第一步：检查库存，并修改库存量
        Good newGood = goodService.getGoodByGoodId(good.getGoodId());
      ///  good.setVersion(newGood.getVersion());
        if(newGood.getQuantity()>0 ){
            int updateStatus= goodService.updateGoodInfo(newGood);
            if(updateStatus>0){

                // 第二步：添加交易订单
                GoodOrder goodOrder =  new GoodOrder();
                goodOrder.setGoodId(good.getGoodId());
                goodOrder.setOrderQuantity(good.getQuantity());
                goodOrder.setOrderTime(CommonUtil.dateTimeToStr(LocalDateTime.now()));
                goodService.addGoodOrder(goodOrder);
                map.put("result","秒杀成功，秒杀时间："+goodOrder.getOrderTime()+",秒杀数量："+goodOrder.getOrderQuantity()
                        +"，库存量："+newGood.getQuantity());
           }

        }else{
              map.put( "result","秒杀失败！！！");
        }
        return map;
    }
    @PostMapping("/seckillRedisLock")
    public String seckilByRedisLock(Integer goodId, Integer quantity,Integer userId) throws Exception {
       if(!goodService.seckilByRedisLock(goodId,quantity,userId)){
           return  "秒杀失败！";
       }else{
           return  "秒杀成功！";
       }
    }
    @PostMapping("/seckillZkLock")
    public String seckillZkLock(Integer goodId, Integer quantity,Integer userId) throws Exception {
        if(!goodService.seckilByZookeeper(goodId,quantity,userId)){
            return  "秒杀失败！";
        }else{
            return  "秒杀成功！";
        }
    }
    @PostMapping("/tran")
    //默认开启，事务回滚
    //@Transactional(rollbackFor = Exception.class)
    public void testTran(){
        try{
            for(int i=0;i<100000;i++){
                User user = new User();
                user.setUserName("测试用户"+i);
                 //user.setUserType(i);
                 user.setLogName("test"+i);
                 user.setCreateTime(CommonUtil.getNowDateTime());
                 user.setPassword("test"+i);
                 user.setUnitId(i);
                userService.addUser(user);

            }
        }catch (Exception e){
            e.printStackTrace();

        }

    }
}
